package com.jml.对象结构;


import org.openjdk.jol.info.ClassLayout;

public class JmlLock {
    private int userId;

    public static void main(String[] args) {
        JmlLock jmlLock = new JmlLock();
        //打印jmlLock对象一共占多少字节
        System.out.println(ClassLayout.parseInstance(jmlLock).toPrintable());
        /**
         * 开启压缩
         * com.jml.对象结构.JmlLock object internals:
         *  OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
         *       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
         *       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
         *       8     4        (object header)                           05 c1 00 f8 (00000101 11000001 00000000 11111000) (-134168315)
         *      12     4    int JmlLock.userId                            0
         * Instance size: 16 bytes
         * Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
         *
         * 对象总共占16字节，对象头占用4*3=12，int变量占用4个字节
         * 正常来说，对象头包括Make word+ class pointer = 16个字节，这里只是12个字节，因为虚拟机默认做了指针压缩
         *
         */

        //-XX:+UseCompressedOops 开启指针压缩
        //-XX:-UseCompressedOops  不开启指针压缩

        /**
         * 不开启压缩，对象头是16，对象正常应该是20个字节，
         * 但是这里做了自动填充，对象字节数要是8的倍数，所以多填充了4个字节
         * com.jml.对象结构.JmlLock object internals:
         *  OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
         *       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
         *       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
         *       8     4        (object header)                           28 30 f7 1b (00101000 00110000 11110111 00011011) (469184552)
         *      12     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
         *      16     4    int JmlLock.userId                            0
         *      20     4        (loss due to the next object alignment)
         * Instance size: 24 bytes
         */
        System.out.println(jmlLock.hashCode());
        System.out.println(Integer.toHexString(jmlLock.hashCode()));
    }
}
